<?php

namespace Wap\Controller;

use User\Model\FactoryModel;
use User\Model\OptionModel;
use Wap\Service\WeixinService;

//header('Access-Control-Allow-Origin: *');

class ToolController extends WapController
{

    public $wxTestToken;

    public function __construct()
    {
        parent::__construct();
        $this->wxTestToken = 'iequde1415960341';        //浙报新媒体
        if (!headers_sent() && // 如果页面头部信息还没有输出--
            extension_loaded("zlib") && // 而且php已经加载了zlib扩展--
            strstr($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip")
        ) //而且浏览器接受GZIP--
        {
            ini_set('zlib.output_compression', 'On');
            ini_set('zlib.output_compression_level', '4');
        }
        /*$cond['phone'] = "15858173759";
        $cond['status'] = 1;
        $user = M("ins_employee")->where($cond)->find();
        session("employee", $user);*/
    }

    private function wxshare()
    {
        $wx = WeixinService::getWxuser('iequde1415960341');
        $wxJSApi = WeixinService::buildWXJSApi($wx);
        $data = array(
            'appId' => $wx['appid'],
            'nonceStr' => $wxJSApi['noncestr'],
            'wxTicket' => $wxJSApi['ticket'],
            'timestamp' => $wxJSApi['timestamp'],
            'wxsha1' => $wxJSApi['sha1']
        );
        $this->assign("data", $data);
    }

    //--意见箱开始--
    public function option_index()
    {
        $this->display();
    }

    public function option_login()
    {
        $params = I("get.", "", "");
        if (empty($params['phone'])) {
            $this->ajaxReturn(array("status" => -1, "result" => "请填写手机号码"));
        }
        $cond['telephone'] = $params['phone'];
        $cond['status'] = OptionModel::STATUS_ACTIVE;
        $person = M("option_person")->where($cond)->find();
        if (empty($person)) {
            $this->ajaxReturn(array("status" => -2, "result" => "登录失败，请检查手机号码/短信验证码"));
        }

        if (empty($params['sms'])) {
            $this->ajaxReturn(array("status" => -3, "result" => "短信验证码不正确"));
        }
        $sms = session("option_sms");
        if ($sms != strtoupper($params['sms'])) {
            $this->ajaxReturn(array("status" => -4, "result" => "短信验证码不正确!"));
        }
        session("person_id", $person['person_id']);
        $this->ajaxReturn(array("status" => $person['level'], "result" => "成功"));
    }

    public function option_get_sms()
    {
        $phone = I("get.phone", "", "");
        if (empty($phone)) {
            $this->ajaxReturn(array('status' => -1, 'result' => "请填写电话号码"));
        }
        $authcode = '';
        for ($i = 0; $i < 4; $i++) {
            $randAsciiNumArray = array(rand(48, 57), rand(65, 90));
            $randAsciiNum = $randAsciiNumArray [rand(0, 1)];
            $randStr = chr($randAsciiNum);
            $authcode .= $randStr;
        }
        $msg = "浙报信息，您的验证码为：" . $authcode;
        session("option_sms", $authcode);
        $result = FactoryModel::sendMsg($phone, $msg);
        $this->ajaxReturn(array('status' => 1, 'result' => $result));
    }

    public function option_view()
    {
        $person_id = session("person_id");
        if (empty($person_id)) {
            return $this->redirect("/Wap/Tool/option_index");
        }
        $cond_leader['status'] = OptionModel::STATUS_ACTIVE;
        $cond_leader['level'] = OptionModel::OPTION_LEVEL_LEADER;
        $leader_arr = M("option_person")->where($cond_leader)->order("person_id desc")->getField("person_id, name");
        $this->assign("leader_arr", $leader_arr);
        $this->display();
    }

    public function option_view_add()
    {
        $data['staff_id'] = session("person_id");
        if (empty($data['staff_id'])) {
            $this->ajaxReturn(['code' => -1]);
        }
        $data['leader_id'] = I("post.leader_id", 0, '');
        if (empty($data['leader_id'])) {
            $this->ajaxReturn(['code' => 0, 'msg' => '请选择领导']);
        }
        $data['content'] = I("post.content", '', '');
        if (empty($data['content'])) {
            $this->ajaxReturn(['code' => 0, 'msg' => '请输入留言内容']);
        }
        $data['ireal_name'] = I("post.ireal_name", 0, "");
        M("option_view")->add($data);
        $this->ajaxReturn(['code' => 1, 'msg' => '成功']);
    }

    public function option_reply_add()
    {
        $person_id = session("person_id");
        if (empty($person_id)) {
            $this->ajaxReturn(['code' => -1]);
        }
        $data['content'] = I("get.content", '', '');
        if (empty($data['content'])) {
            $this->ajaxReturn(['code' => 0]);
        }
        $data['view_id'] = I("get.view_id", 0, '');
        M("option_resp")->add($data);
        $this->ajaxReturn(['code' => 1, 'msg' => '成功']);
    }

    public function option_delete()
    {
        $person_id = session("person_id");
        if (empty($person_id)) {
            $this->ajaxReturn("1");
        }
        $view_id = I("get.view_id", 0, '');
        $resp_id = I("get.resp_id", 0, '');
        if (!empty($view_id)) {
            $cond['view_id'] = $view_id;
            $data['status'] = OptionModel::STATUS_DELETE;
            M("option_view")->where($cond)->save($data);
        } elseif (!empty($resp_id)) {
            $cond['resp_id'] = $resp_id;
            $data['status'] = OptionModel::STATUS_DELETE;
            M("option_resp")->where($cond)->save($data);
        }
        $this->ajaxReturn("1");
    }

    public function option_reply()
    {
        $person_id = session("person_id");
        if (empty($person_id)) {
            return $this->redirect("/Wap/Tool/option_index");
        }
        $cond['person_id'] = $person_id;
        $cond['status'] = OptionModel::STATUS_ACTIVE;
        $person = M("option_person")->where($cond)->find();
        $view_arr = array();
        $person_arr = array();
        $reply_arr = array();
        $this->assign("person", $person);
        if (!empty($person)) {
            //---员工--
            if ($person['level'] == OptionModel::OPTION_LEVEL_STAFF) {
                //--获取意见列表---
                $cond_view['status'] = OptionModel::STATUS_ACTIVE;
                $cond_view['staff_id'] = $person['person_id'];
                $view_arr = M("option_view")->where($cond_view)->order("view_id desc")->select();

                //--获取领导名称及回复内容--
                $leader_ids = array();
                $view_ids = array();
                foreach ($view_arr as $view) {
                    $leader_ids[] = $view['leader_id'];
                    $view_ids[] = $view['view_id'];
                }
                if (!empty($leader_ids)) {
                    $leader_ids = array_unique($leader_ids);
                    $cond_leader['person_id'] = array("IN", $leader_ids);
                    $person_arr = M("option_person")->where($cond_leader)->getField("person_id,name");
                }
                if (!empty($view_ids)) {
                    $view_ids = array_unique($view_ids);
                    $cond_reply['view_id'] = array("IN", $view_ids);
                    $cond_reply['status'] = OptionModel::STATUS_ACTIVE;
                    $reply_arr = M("option_resp")->where($cond_reply)->select();
                }
            } else {//--领导--
                //--获取意见列表---
                $cond_view['status'] = OptionModel::STATUS_ACTIVE;
                $cond_view['leader_id'] = $person['person_id'];
                $view_arr = M("option_view")->where($cond_view)->order("view_id desc")->select();
                //--获取员工名称及回复内容--
                $staff_ids = array();
                $view_ids = array();
                foreach ($view_arr as $view) {
                    $staff_ids[] = $view['staff_id'];
                    $view_ids[] = $view['view_id'];
                }

                if (!empty($staff_ids)) {
                    $staff_ids = array_unique($staff_ids);
                    $cond_staff['person_id'] = array("IN", $staff_ids);
                    $person_arr = M("option_person")->where($cond_staff)->getField("person_id,name");
                }
                if (!empty($view_ids)) {
                    $view_ids = array_unique($view_ids);
                    $cond_reply['view_id'] = array("IN", $view_ids);
                    $cond_reply['status'] = OptionModel::STATUS_ACTIVE;
                    $reply_arr = M("option_resp")->where($cond_reply)->select();
                }
            }
        }
        $this->assign("view_arr", $view_arr);
        $this->assign("person_arr", $person_arr);
        $this->assign("reply_arr", $reply_arr);

        $this->display();
    }

    //--意见箱结束--


    //----淘志愿报考指南开始----
    public function bkzn()
    {
        FactoryModel::viewPage(5);
        $this->wxshare();
        return $this->display();
    }

    public function bkselect()
    {
        FactoryModel::viewPage(5);
        $this->wxshare();
        return $this->display();
    }

    public function bklist()
    {
        FactoryModel::viewPage(5);
        $this->wxshare();
        $cond['status'] = 1;
        $spec_cond_list = S("spec_cond");
        if (empty($spec_cond_list)) {
            $spec_cond_list = M("tzy_bkzn_cond")->where($cond)->select();
            S("spec_cond", $spec_cond_list);
        }
        $data['a'] = 0;
        $params = I("get.", "", "");
        switch ($params['yw']) {
            case 'A':
                $data['a']++;
                $data['b']++;
                $data['c']++;
                $data['d']++;
                $data['abp']++;
                $data['yw'] = 1;
                break;
            case 'B':
                $data['b']++;
                $data['c']++;
                $data['d']++;
                $data['abp']++;
                $data['yw'] = 2;
                break;
            case 'C':
                $data['c']++;
                $data['d']++;
                $data['yw'] = 3;
                break;
            case 'D':
                $data['d']++;
                $data['yw'] = 4;
                break;
        }
        switch ($params['sx']) {
            case 'A':
                $data['a']++;
                $data['b']++;
                $data['c']++;
                $data['d']++;
                $data['abp']++;
                $data['sx'] = 1;
                break;
            case 'B':
                $data['b']++;
                $data['c']++;
                $data['d']++;
                $data['abp']++;
                $data['sx'] = 2;
                break;
            case 'C':
                $data['c']++;
                $data['d']++;
                $data['sx'] = 3;
                break;
            case 'D':
                $data['d']++;
                $data['sx'] = 4;
                break;
        }
        switch ($params['yy']) {
            case 'A':
                $data['a']++;
                $data['b']++;
                $data['c']++;
                $data['d']++;
                $data['abp']++;
                $data['yy'] = 1;
                break;
            case 'B':
                $data['b']++;
                $data['c']++;
                $data['d']++;
                $data['abp']++;
                $data['yy'] = 2;
                break;
            case 'C':
                $data['c']++;
                $data['d']++;
                $data['yy'] = 3;
                break;
            case 'D':
                $data['d']++;
                $data['yy'] = 4;
                break;
        }
        switch ($params['sz']) {
            case 'A':
                $data['a']++;
                $data['b']++;
                $data['c']++;
                $data['d']++;
                $data['abp']++;
                $data['sz'] = 1;
                break;
            case 'B':
                $data['b']++;
                $data['c']++;
                $data['d']++;
                $data['abp']++;
                $data['sz'] = 2;
                break;
            case 'C':
                $data['c']++;
                $data['d']++;
                $data['sz'] = 3;
                break;
            case 'D':
                $data['d']++;
                $data['sz'] = 4;
                break;
        }
        switch ($params['ls']) {
            case 'A':
                $data['a']++;
                $data['b']++;
                $data['c']++;
                $data['d']++;
                $data['abp']++;
                $data['ls'] = 1;
                break;
            case 'B':
                $data['b']++;
                $data['c']++;
                $data['d']++;
                $data['abp']++;
                $data['ls'] = 2;
                break;
            case 'C':
                $data['c']++;
                $data['d']++;
                $data['ls'] = 3;
                break;
            case 'D':
                $data['d']++;
                $data['ls'] = 4;
                break;
        }
        switch ($params['dl']) {
            case 'A':
                $data['a']++;
                $data['b']++;
                $data['c']++;
                $data['d']++;
                $data['abp']++;
                $data['dl'] = 1;
                break;
            case 'B':
                $data['b']++;
                $data['c']++;
                $data['d']++;
                $data['abp']++;
                $data['dl'] = 2;
                break;
            case 'C':
                $data['c']++;
                $data['d']++;
                $data['dl'] = 3;
                break;
            case 'D':
                $data['d']++;
                $data['dl'] = 4;
                break;
        }
        switch ($params['wl']) {
            case 'A':
                $data['a']++;
                $data['b']++;
                $data['c']++;
                $data['d']++;
                $data['abp']++;
                $data['wl'] = 1;
                break;
            case 'B':
                $data['b']++;
                $data['c']++;
                $data['d']++;
                $data['abp']++;
                $data['wl'] = 2;
                break;
            case 'C':
                $data['c']++;
                $data['d']++;
                $data['wl'] = 3;
                break;
            case 'D':
                $data['d']++;
                $data['wl'] = 4;
                break;
        }
        switch ($params['hx']) {
            case 'A':
                $data['a']++;
                $data['b']++;
                $data['c']++;
                $data['d']++;
                $data['abp']++;
                $data['hx'] = 1;
                break;
            case 'B':
                $data['b']++;
                $data['c']++;
                $data['d']++;
                $data['abp']++;
                $data['hx'] = 2;
                break;
            case 'C':
                $data['c']++;
                $data['d']++;
                $data['hx'] = 3;
                break;
            case 'D':
                $data['d']++;
                $data['hx'] = 4;
                break;
        }
        switch ($params['sw']) {
            case 'A':
                $data['a']++;
                $data['b']++;
                $data['c']++;
                $data['d']++;
                $data['abp']++;
                $data['sw'] = 1;
                break;
            case 'B':
                $data['b']++;
                $data['c']++;
                $data['d']++;
                $data['abp']++;
                $data['sw'] = 2;
                break;
            case 'C':
                $data['c']++;
                $data['d']++;
                $data['sw'] = 3;
                break;
            case 'D':
                $data['d']++;
                $data['sw'] = 4;
                break;
        }
        switch ($params['js']) {
            case 'A':
                $data['a']++;
                $data['b']++;
                $data['c']++;
                $data['d']++;
                $data['abp']++;
                $data['js'] = 1;
                break;
            case 'B':
                $data['b']++;
                $data['c']++;
                $data['d']++;
                $data['abp']++;
                $data['js'] = 2;
                break;
            case 'C':
                $data['c']++;
                $data['d']++;
                $data['js'] = 3;
                break;
            case 'D':
                $data['d']++;
                $data['js'] = 4;
                break;
            default:
                ;
        }
        $spec_list = array();
        $sch_list = array();
        $sch_id_list = array();
        foreach ($spec_cond_list as $spec_cond) {
            if ($spec_cond['ap'] > $data['a']) {
                continue;
            }
            if ($spec_cond['bp'] > $data['b']) {
                continue;
            }
            if ($spec_cond['cp'] > $data['c']) {
                continue;
            }
            if ($spec_cond['dp'] > $data['d']) {
                continue;
            }
            if ($spec_cond['abp'] > $data['abp']) {
                continue;
            }
            if ($spec_cond['szs'] > $data['sz']) {
                continue;
            }

            if ($spec_cond['lss'] > $data['ls']) {
                continue;
            }
            if ($spec_cond['dls'] > $data['dl']) {
                continue;
            }
            if ($spec_cond['wls'] > $data['wl']) {
                continue;
            }

            if ($spec_cond['hxs'] > $data['hx']) {
                continue;
            }
            if ($spec_cond['sws'] > $data['sw']) {
                continue;
            }
            if ($spec_cond['jss'] > $data['js']) {
                continue;
            }
            if ($spec_cond['yys'] > $data['yy']) {
                continue;
            }
            if ($spec_cond['yws'] > $data['yw']) {
                continue;
            }
            if ($spec_cond['sxs'] > $data['sx']) {
                continue;
            }

            $params['szs'] = $params['szs'] ? 1 : 0;
            $params['lss'] = $params['lss'] ? 1 : 0;
            $params['wls'] = $params['wls'] ? 1 : 0;
            $params['hxs'] = $params['hxs'] ? 1 : 0;
            $params['sws'] = $params['sws'] ? 1 : 0;
            $params['jss'] = $params['jss'] ? 1 : 0;
            $params['dls'] = $params['dls'] ? 1 : 0;
            $must_yes = 0;
            if (($spec_cond['sz_must'] == 1) && ($spec_cond['sz_must'] == $params['szs'])) {
                $must_yes = 1;
            }
            if (($spec_cond['ls_must'] == 1) && ($spec_cond['ls_must'] == $params['lss'])) {
                $must_yes = 1;
            }
            if (($spec_cond['dl_must'] == 1) && ($spec_cond['dl_must'] == $params['dls'])) {
                $must_yes = 1;
            }
            if (($spec_cond['wl_must'] == 1) && ($spec_cond['wl_must'] == $params['wls'])) {
                $must_yes = 1;
            }
            if (($spec_cond['hx_must'] == 1) && ($spec_cond['hx_must'] == $params['hxs'])) {
                $must_yes = 1;
            }
            if (($spec_cond['sw_must'] == 1) && ($spec_cond['sw_must'] == $params['sws'])) {
                $must_yes = 1;
            }
            if (($spec_cond['js_must'] == 1) && ($spec_cond['js_must'] == $params['jss'])) {
                $must_yes = 1;
            }
            if (($spec_cond['sz_must'] == 0) && ($spec_cond['ls_must'] == 0) & ($spec_cond['dl_must'] == 0) && ($spec_cond['wl_must'] == 0) &
                ($spec_cond['hx_must'] == 0) & ($spec_cond['sw_must'] == 0) && ($spec_cond['js_must'] == 0)
            ) {
                $must_yes = 1;
            }
            if ($must_yes == 0) {
                continue;
            }
            if ($spec_cond['score'] > 0) {
                $score = 0;
                switch ($params['yw']) {
                    case "A":
                        $score += $spec_cond['ywa'];
                        break;
                    case "B":
                        $score += $spec_cond['ywb'];
                        break;
                    case "C":
                        $score += $spec_cond['ywc'];
                        break;
                    case "D":
                        $score += $spec_cond['ywd'];
                        break;
                    default:
                        ;
                }
                switch ($params['sz']) {
                    case "A":
                        $score += $spec_cond['sza'];
                        break;
                    case "B":
                        $score += $spec_cond['szb'];
                        break;
                    case "C":
                        $score += $spec_cond['szc'];
                        break;
                    case "D":
                        $score += $spec_cond['szd'];
                        break;
                    default:
                        ;
                }
                switch ($params['ls']) {
                    case "A":
                        $score += $spec_cond['lsa'];
                        break;
                    case "B":
                        $score += $spec_cond['lsb'];
                        break;
                    case "C":
                        $score += $spec_cond['lsc'];
                        break;
                    case "D":
                        $score += $spec_cond['lsd'];
                        break;
                    default:
                        ;
                }
                switch ($params['dl']) {
                    case "A":
                        $score += $spec_cond['dla'];
                        break;
                    case "B":
                        $score += $spec_cond['dlb'];
                        break;
                    case "C":
                        $score += $spec_cond['dlc'];
                        break;
                    case "D":
                        $score += $spec_cond['dld'];
                        break;
                    default:
                        ;
                }
                switch ($params['wl']) {
                    case "A":
                        $score += $spec_cond['wla'];
                        break;
                    case "B":
                        $score += $spec_cond['wlb'];
                        break;
                    case "C":
                        $score += $spec_cond['wlc'];
                        break;
                    case "D":
                        $score += $spec_cond['wld'];
                        break;
                    default:
                        ;
                }
                switch ($params['hx']) {
                    case "A":
                        $score += $spec_cond['hxa'];
                        break;
                    case "B":
                        $score += $spec_cond['hxb'];
                        break;
                    case "C":
                        $score += $spec_cond['hxc'];
                        break;
                    case "D":
                        $score += $spec_cond['hxd'];
                        break;
                    default:
                        ;
                }
                switch ($params['sw']) {
                    case "A":
                        $score += $spec_cond['swa'];
                        break;
                    case "B":
                        $score += $spec_cond['swb'];
                        break;
                    case "C":
                        $score += $spec_cond['swc'];
                        break;
                    case "D":
                        $score += $spec_cond['swd'];
                        break;
                    default:
                        ;
                }
                switch ($params['js']) {
                    case "A":
                        $score += $spec_cond['jsa'];
                        break;
                    case "B":
                        $score += $spec_cond['jsb'];
                        break;
                    case "C":
                        $score += $spec_cond['jsc'];
                        break;
                    case "D":
                        $score += $spec_cond['jsd'];
                        break;
                    default:
                        ;
                }
                switch ($params['yy']) {
                    case "A":
                        $score += $spec_cond['yya'];
                        break;
                    case "B":
                        $score += $spec_cond['yyb'];
                        break;
                    case "C":
                        $score += $spec_cond['yyc'];
                        break;
                    case "D":
                        $score += $spec_cond['yyd'];
                        break;
                    default:
                        ;
                }
                switch ($params['sx']) {
                    case "A":
                        $score += $spec_cond['sxa'];
                        break;
                    case "B":
                        $score += $spec_cond['sxb'];
                        break;
                    case "C":
                        $score += $spec_cond['sxc'];
                        break;
                    case "D":
                        $score += $spec_cond['sxd'];
                        break;
                    default:
                        ;
                }
                if ($score < $spec_cond['score']) {
                    continue;
                }
            }

            $spec_list[$spec_cond['spec_id']] = 1;
        }
        $count = 0;
        if (!empty($spec_list)) {
            $spec_list = array_keys($spec_list);
            $spec_cond_sel['spec_id'] = array("IN", $spec_list);
            $spec_list = M("tzy_bkzn_spec")->where($spec_cond_sel)->order("code asc")->select();

            foreach ($spec_list as $spec) {
                $sch_list[] = $spec['sch_id'];
            }
            $sch_cond['s_id'] = array("IN", $sch_list);
            $sch_list = M("tzy_bkzn_school")->where($sch_cond)->order("code asc")->getField("s_id, name, code");
            $count = count($sch_list);
            foreach ($spec_list as $spec) {
                if (empty($sch_list[$spec['sch_id']]['spec_list'])) {
                    $sch_list[$spec['sch_id']]['spec_list'] = array($spec);
                } else {
                    $sch_list[$spec['sch_id']]['spec_list'][] = $spec;
                }
            }
        }
        $this->assign("count", $count);
        $this->assign("sch_list", $sch_list);
        return $this->display();
    }

    public function bkdetail()
    {
        FactoryModel::viewPage(5);
        $this->wxshare();
        $cond['s_id'] = I("get.schId", "", "");
        $school = M("tzy_bkzn_school")->where($cond)->find();

        $cond_spec['spec_id'] = I("get.specId", "", "");
        $spec = M("tzy_bkzn_spec")->where($cond_spec)->find();
        $this->assign("school", $school);
        $this->assign("spec", $spec);
        return $this->display();
    }

    public function tzy_add()
    {
        set_time_limit(0);
        vendor('phpexcel.PHPExcel');
        $PHPReader = new \PHPExcel();
        $filePath = 'test.xlsx';
        if (empty($filePath) or !file_exists($filePath)) {
            die('file not exists');
        }
        $objReader = \PHPExcel_IOFactory::createReader("Excel2007");
        $objPHPExcel = $objReader->load($filePath);
        $currentSheet = $objPHPExcel->getSheet(3);
        $allColumn = $currentSheet->getHighestColumn();
        $allRow = $currentSheet->getHighestRow();
        $data = array();
        for ($rowIndex = 2; $rowIndex <= $allRow; $rowIndex++) {        //循环读取每个单元格的内容。注意行从1开始，列从A开始
            $data_unit = array();
            for ($colIndex = 'A'; $colIndex <= $allColumn; $colIndex++) {
                $addr = $colIndex . $rowIndex;
                $cell = $currentSheet->getCell($addr)->getValue();
                if ($cell instanceof PHPExcel_RichText) { //富文本转换字符串
                    $cell = $cell->__toString();
                }
                switch ($colIndex) {
                    case "A":
                        $data_unit['code'] = $cell;
                        break;
                    case "B":
                        $data_unit['name'] = $cell;
                        break;
                    case "C":
                        $data_unit['spec'] = $cell;
                        break;
                    case "D":
                        $data_unit['spec_name'] = $cell;
                        break;
                    case "E":
                        $data_unit['plan2018'] = $cell;
                        break;
                    case "F":
                        $data_unit['fs2018'] = $cell;
                        break;
                    case "G":
                        $data_unit['wc2018'] = $cell;
                        break;
                    default:
                        ;
                }
            }
            $data[] = $data_unit;
            if (!($rowIndex % 100) || ($rowIndex == $allRow)) {
                M("test_school")->addAll($data);
                $data = array();
            }
        }
        return $data;
    }

    public function tzy_update()
    {
        set_time_limit(0);
        vendor('phpexcel.PHPExcel');
        $PHPReader = new \PHPExcel();
        $filePath = 'test.xlsx';
        if (empty($filePath) or !file_exists($filePath)) {
            die('file not exists');
        }
        $objReader = \PHPExcel_IOFactory::createReader("Excel2007");
        $objPHPExcel = $objReader->load($filePath);
        $currentSheet = $objPHPExcel->getSheet(0);
        $allColumn = $currentSheet->getHighestColumn();
        $allRow = $currentSheet->getHighestRow();
        for ($rowIndex = 2; $rowIndex <= $allRow; $rowIndex++) {        //循环读取每个单元格的内容。注意行从1开始，列从A开始
            $data_unit = array();
            $addr = "A" . $rowIndex;
            $where['code'] = $currentSheet->getCell($addr)->getValue();
            if ($where['code'] instanceof PHPExcel_RichText) { //富文本转换字符串
                $where['code'] = $where['code']->__toString();
            }
            $addr = "D" . $rowIndex;
            $where['spec_name'] = $currentSheet->getCell($addr)->getValue();
            if ($where['spec_name'] instanceof PHPExcel_RichText) { //富文本转换字符串
                $where['spec_name'] = $where['spec_name']->__toString();
            }
            $school = M("test_school")->where($where)->find();
            if (empty($school)) {
                continue;
            }
            for ($colIndex = 'E'; $colIndex <= 'G'; $colIndex++) {
                $addr = $colIndex . $rowIndex;
                $cell = $currentSheet->getCell($addr)->getValue();
                if ($cell instanceof PHPExcel_RichText) { //富文本转换字符串
                    $cell = $cell->__toString();
                }

                switch ($colIndex) {
                    case "E":
                        $data_unit['plan2017'] = $cell;
                        break;
                    case "F":
                        $data_unit['fs2017'] = $cell;
                        break;
                    case "G":
                        $data_unit['wc2017'] = $cell;
                        break;
                    default:
                        ;
                }
            }
            if (($school['wc2017'] == 0) || ($data_unit['wc2017'] < $school['wc2017'])) {
                M("test_school")->where(['id' => $school['id']])->save($data_unit);
            }
        }
        return;
    }

    public function tzy_export()
    {
        set_time_limit(0);
        vendor('phpexcel.PHPExcel');
        $objExcel = new \PHPExcel();
        $objActSheet = $objExcel->getActiveSheet();
        $objActSheet->getColumnDimension('A')->setWidth(30);
        $objActSheet->getColumnDimension('B')->setWidth(30);
        $objActSheet->getColumnDimension('C')->setWidth(30);
        $objActSheet->getColumnDimension('D')->setWidth(20);
        $objActSheet->getColumnDimension('E')->setWidth(20);
        $objActSheet->getColumnDimension('F')->setWidth(30);
        $objActSheet->getColumnDimension('G')->setWidth(30);
        $objActSheet->getColumnDimension('H')->setWidth(20);
        $objActSheet->setCellValue('A1', "学校代号");
        $objActSheet->setCellValue('B1', "学校名称");
        $objActSheet->setCellValue('C1', "专业代号");
        $objActSheet->setCellValue('D1', "专业名称");
        $objActSheet->setCellValue('E1', "2018计划数");
        $objActSheet->setCellValue('F1', "2018分数线");
        $objActSheet->setCellValue('G1', "2018位次");
        $objActSheet->setCellValue('H1', "2017计划数");
        $objActSheet->setCellValue('I1', "2017分数线");
        $objActSheet->setCellValue('J1', "2017位次");
        $line = 2;
        $count = M("test_school")->count();
        $number = $count / 100;
        for ($i = 0; $i <= $number; $i++) {
            $list = M("test_school")->limit($i * 100, 100)->order('id asc')->select();
            if (!empty($list)) {
                foreach ($list as $value) {
                    $objActSheet->setCellValue("A" . $line, $value['code']);
                    $objActSheet->setCellValue("B" . $line, $value['name']);
                    $objActSheet->setCellValue("C" . $line, $value['spec']);
                    $objActSheet->setCellValue("D" . $line, $value['spec_name']);
                    $objActSheet->setCellValue("E" . $line, $value['plan2018']);
                    $objActSheet->setCellValue("F" . $line, $value['fs2018']);
                    $objActSheet->setCellValue("G" . $line, $value['wc2018']);
                    $objActSheet->setCellValue("H" . $line, $value['plan2017']);
                    $objActSheet->setCellValue("I" . $line, $value['fs2017']);
                    $objActSheet->setCellValue("J" . $line, $value['wc2017']);
                    $line++;
                }
            }
        }
        $fileName = '2017-2018新' . '.xlsx';
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');
        $objWriter = new \PHPExcel_Writer_Excel2007($objExcel);
        $objWriter->save('php://output');
    }

    public function get_school_list()
    {
        set_time_limit(0);
        $fcontents = file_get_contents("http://zt.zjzs.net/xk2019/subject_0.html");
        $match_param = '/<a href="(.*?)"[^>]*>[^>]*<\/a>/si';
        $table_data = preg_match_all($match_param, $fcontents, $match);
        vendor('phpexcel.PHPExcel');
        $objExcel = new \PHPExcel();
        $objActSheet = $objExcel->getActiveSheet();
        $objActSheet->getColumnDimension('A')->setWidth(30);
        $objActSheet->getColumnDimension('B')->setWidth(30);
        $objActSheet->getColumnDimension('C')->setWidth(30);
        $objActSheet->getColumnDimension('D')->setWidth(20);
        $objActSheet->getColumnDimension('E')->setWidth(20);
        $objActSheet->setCellValue('A1', "学校名称");
        $objActSheet->setCellValue('B1', "层次");
        $objActSheet->setCellValue('C1', "专业名称");
        $objActSheet->setCellValue('D1', "专业方向");
        $objActSheet->setCellValue('E1', "选考科目");
        $line = 2;
//        var_dump(count($match[1]));return;
        foreach ($match[1] as $k => $v) {
            if ($k == 1000) {
                break;
            }
            if ($k > 0) {
                $list = $this->get_school_data("http://zt.zjzs.net/xk2019/" . $v);
                foreach ($list as $value) {
                    $objActSheet->setCellValue("A" . $line, $value[4]);
                    $objActSheet->setCellValue("B" . $line, $value[0]);
                    $en_min_pos = strpos($value[1], '(');
                    $cn_min_pos = strpos($value[1], '（');
                    $min_pos = 0;
                    if (($en_min_pos != 0) && ($cn_min_pos != 0)) {
                        $min_pos = min($en_min_pos, $cn_min_pos);
                    } elseif (($en_min_pos != 0) && ($cn_min_pos == 0)) {
                        $min_pos = $en_min_pos;
                    } elseif (($en_min_pos == 0) && ($cn_min_pos != 0)) {
                        $min_pos = $cn_min_pos;
                    }
                    $en_max_pos = strrpos($value[1], ')');
                    $cn_max_pos = strrpos($value[1], '）');
                    $max_pos = 0;
                    if (($en_max_pos != 0) && ($cn_max_pos != 0)) {
                        $max_pos = max($en_max_pos, $cn_max_pos);
                    } elseif (($en_max_pos != 0) && ($cn_max_pos == 0)) {
                        $max_pos = $en_max_pos;
                    } elseif (($en_max_pos == 0) && ($cn_max_pos != 0)) {
                        $max_pos = $cn_max_pos;
                    }

                    if (($min_pos != 0) && ($max_pos != 0)) {
                        $v_c = substr($value[1], 0, $min_pos);
                        $objActSheet->setCellValue("C" . $line, $v_c);
                        $v_d = substr($value[1], $min_pos + 1, $max_pos - $min_pos - 1);
                        $objActSheet->setCellValue("D" . $line, $v_d);
                    } else {
                        $objActSheet->setCellValue("C" . $line, $value[1]);
                        $objActSheet->setCellValue("D" . $line, "");
                    }
                    $objActSheet->setCellValue("E" . $line, $value[3]);
                    $line++;
                }
            }
        }
        $fileName = '2017-2018新' . '.xlsx';
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');
        $objWriter = new \PHPExcel_Writer_Excel2007($objExcel);
        $objWriter->save('php://output');
    }

    public function get_school_data($url = '')
    {
        set_time_limit(0);
        $fcontents = file_get_contents($url);
        $match_school = '/<div[^>]*>(.*?)<\/div>/si';
        $school_data = preg_match_all($match_school, $fcontents, $match_school);
        $match_school[0][2] = preg_replace('/\s(?=\s)/', '', trim(strip_tags($match_school[0][2])));
        $school = explode("：", $match_school[0][2]);
        $match_param = '/<table[^>]*>(.*?)<\/table>/si';
        $table_data = preg_match_all($match_param, $fcontents, $match);

        $table_data = $match[0][0];
        $table_array = explode('<tr>', $table_data);
        $data = array();
        for ($i = 0; $i < (count($table_array) - 1); $i++) {
            if ($i == 0) {
                $table_array[$i] = explode('<th>', $table_array[$i]);
                $table_array[$i] = $table_array[$i][5];
                $table_array[$i] = explode('</td>', $table_array[$i]);
                $data[0][0] = preg_replace('/\s(?=\s)/', '', trim(strip_tags($table_array[$i][1])));
                $data[0][1] = preg_replace('/\s(?=\s)/', '', trim(strip_tags($table_array[$i][2])));
                $data[0][2] = preg_replace('/\s(?=\s)/', '', trim(strip_tags($table_array[$i][3])));
                $data[0][3] = preg_replace('/\s(?=\s)/', '', trim(strip_tags($table_array[$i][4])));
//                $data[0][4] = preg_replace('/\s(?=\s)/', '', trim(strip_tags($table_array[$i][5])));
                $data[0][] = $school[1];
                continue;
            }
            $data[$i] = explode('</td>', $table_array[$i]);

            for ($j = 0; $j < count($data[$i]); $j++) {
                $data[$i][$j] = preg_replace('/\s(?=\s)/', '', trim(strip_tags($data[$i][$j])));
            }
            array_pop($data[$i]);
            array_pop($data[$i]);
            $data[$i][] = $school[1];
        }
        return $data;
    }

    //---登录页面---
    public function login()
    {
        $wx = WeixinService::getWxuser($this->wxTestToken);
        $wxJSApi = WeixinService::buildWXJSApi($wx);
        $wxConfig = array(
            'appId' => $wx['appid'],
            'timestamp' => $wxJSApi['timestamp'],
            'nonceStr' => $wxJSApi['noncestr'],
            'signature' => $wxJSApi['sha1']
        );
        $this->assign('wxConfig', $wxConfig);

        $user = session("employee");
        if (empty($user)) {
            $this->assign('isLogin', '0');
            $this->assign("name", "");
        } else {
            $this->assign('isLogin', '1');
            $this->assign("name", $user['name']);
        }
        $this->display();
    }

    //----获取短信---
    public function ajaxSms()
    {
        $phone = I("get.phone", "", "");
        if (empty($phone)) {
            $this->ajaxReturn(array('code' => -1, 'result' => "请填写电话号码"));
        }
        $authcode = '';
        for ($i = 0; $i < 4; $i++) {
            $randAsciiNumArray = array(rand(48, 57), rand(65, 90));
            $randAsciiNum = $randAsciiNumArray [rand(0, 1)];
            $randStr = chr($randAsciiNum);
            $authcode .= $randStr;
        }
        $msg = "浙报信息，您的验证码为：" . $authcode;
        session("sms", $authcode);
        $result = FactoryModel::sendMsg($phone, $msg);
        $this->ajaxReturn(array('code' => 1, 'result' => $result));
    }

    //---登录验证---
    public function ajaxLogin()
    {
        $params = I("post.", "", "");
        if (empty($params['phone'])) {
            $this->ajaxReturn(array("code" => -1, "result" => "请填写手机号码"));
        }
        $cond['phone'] = $params['phone'];
        $cond['status'] = 1;
        $user = M("ins_employee")->where($cond)->find();
        if (empty($user)) {
            $this->ajaxReturn(array("code" => -2, "result" => "登录失败，请检查手机号码/短信验证码"));
        }

        if (empty($params['sms'])) {
            $this->ajaxReturn(array("code" => -3, "result" => "短信验证码不正确"));
        }
        $sms = session("sms");
        if ($sms != strtoupper($params['sms'])) {
            $this->ajaxReturn(array("code" => -4, "result" => "短信验证码不正确!"));
        }
        session("employee", $user);
        $this->ajaxReturn(array("code" => 1, "result" => "成功", "name" => $user['name']));
    }

    //----退出登录---------
    public function ajaxLoginOut()
    {
        session('employee', null);
        $this->ajaxReturn(array("code" => 1, "result" => "成功"));
    }

    //-----奖金分配首页------
    public function award()
    {
        $user = session("employee");
        if (empty($user)) {
            //-------跳转登录页--------
            return $this->redirect("/Wap/Tool/login");
        }
        $wx = WeixinService::getWxuser($this->wxTestToken);
        $wxJSApi = WeixinService::buildWXJSApi($wx);
        $wxConfig = array(
            'appId' => $wx['appid'],
            'timestamp' => $wxJSApi['timestamp'],
            'nonceStr' => $wxJSApi['noncestr'],
            'signature' => $wxJSApi['sha1']
        );
        $this->assign('wxConfig', $wxConfig);

        $cond1_room['emp_id'] = $user['id'];
        $cond2_room['_complex'] = $cond1_room;
        $cond2_room['checker'] = $user['id'];
        $cond2_room['_logic'] = 'or';
        $cond3_room['_complex'] = $cond2_room;
        $cond3_room['deside'] = $user['id'];
        $cond3_room['_logic'] = 'or';
        $final['_complex'] = $cond3_room;
        $final['status'] = 1;
        $rooms = M("ins_room")->where($final)->select();
        $rooms = json_encode($rooms);
        $name = json_encode($user['name']);
        $this->assign("rooms", $rooms);
        $this->assign("name", $name);

        $cond_dept['status'] = 1;
        $cond_dept['parent_id'] = 0;
        $depts = M("ins_dept")->where($cond_dept)->select();
        foreach ($depts as &$dept) {
            $dept_id_arr = array($dept['id']);
            $this->get_dept_son($dept_id_arr, $dept['id']);
            $cond_emp['dept_id'] = array("IN", $dept_id_arr);
            $cond_emp['status'] = 1;
            $emps = M("ins_employee")->where($cond_emp)->field("id,name")->order("id asc")->select();
            $dept['emps'] = $emps;
        }
        $depts = json_encode($depts);
        $this->assign("depts", $depts);
        $this->display();
    }

    private function get_dept_son(&$dept_id_arr, $dept_id)
    {
        $cond['parent_id'] = $dept_id;
        $cond['status'] = 1;
        $depts = M("ins_dept")->where($cond)->field("id")->select();
        if (!empty($depts)) {
            foreach ($depts as $dept) {
                $dept_id_arr[] = $dept['id'];
                $this->get_dept_son($dept_id_arr, $dept['id']);
            }
        }
    }

    //---------部室奖金明细首页------------
    public function riches()
    {
        $user = session("employee");
        if (empty($user)) {
            //-------跳转登录页--------
            return $this->redirect("/Wap/Tool/login");
        }
        $wx = WeixinService::getWxuser($this->wxTestToken);
        $wxJSApi = WeixinService::buildWXJSApi($wx);
        $wxConfig = array(
            'appId' => $wx['appid'],
            'timestamp' => $wxJSApi['timestamp'],
            'nonceStr' => $wxJSApi['noncestr'],
            'signature' => $wxJSApi['sha1']
        );
        $this->assign('wxConfig', $wxConfig);

        $cond1_room['detail1'] = $user['id'];
        $cond2_room['_complex'] = $cond1_room;
        $cond2_room['detail2'] = $user['id'];
        $cond2_room['_logic'] = 'or';
        $cond3_room['_complex'] = $cond2_room;
        $cond3_room['detail3'] = $user['id'];
        $cond3_room['_logic'] = 'or';
        $final['_complex'] = $cond3_room;
        $final['status'] = 1;
        $rooms = M("ins_room")->where($final)->select();
        $rooms = json_encode($rooms);
        $name = json_encode($user['name']);
        $this->assign("rooms", $rooms);
        $this->assign("name", $name);

        $cond_dept['status'] = 1;
        $cond_dept['parent_id'] = 0;
        $depts = M("ins_dept")->where($cond_dept)->select();
        foreach ($depts as &$dept) {
            $dept_id_arr = array($dept['id']);
            $this->get_dept_son($dept_id_arr, $dept['id']);
            $cond_emp['dept_id'] = array("IN", $dept_id_arr);
            $cond_emp['status'] = 1;
            $emps = M("ins_employee")->where($cond_emp)->field("id,name")->order("id asc")->select();
            $dept['emps'] = $emps;
        }
        $depts = json_encode($depts);
        $this->assign("depts", $depts);
        $this->display();
    }

    //-----部室奖金明细月份-----
    public function ajaxRoomRiches()
    {
        $room_id = I('get.room_id', "", "");
        $month = I("get.month", "", "");
        $year = I("get.year", "", "");
        if (empty($month)) {
            $month = date("n");
        }
        if (empty($year)) {
            $year = date("Y");
        }
        $res = array("code" => -1, "m_start" => 0, "m_end" => 0, "income" => "", "pay" => "");
        //----判断是否为部室管理者----
        $check_limit = $this->check_room_limit($room_id, 4);
        if (!$check_limit) {
            $this->ajaxReturn($res);
        }
        $cond['room_id'] = $room_id;
        $cond['status'] = 1;
        $cond['month'] = $month;
        $cond['year'] = $year;
        $riches = M("ins_riches")->where($cond)->find();
        $res['code'] = 1;
        if (!empty($riches)) {
            $res['code'] = 1;
            $res['m_start'] = $riches['m_start'];
            $res['m_end'] = $riches['m_end'];
            $res['income'] = $riches['income'];
            $res['pay'] = $riches['pay'];
        }
        $this->ajaxReturn($res);
    }

    //----部室奖金分配页----
    public function ajaxRoomAward()
    {

        $room_id = I('get.room_id', "", "");
        $month = I("get.month", "", "");
        $year = I("get.year", "", "");
        if (empty($month)) {
            $month = date("n");
        }
        if (empty($year)) {
            $year = date("Y");
        }
        $res = array("code" => -1, "award" => 0, "fee" => 0, "dtl" => array(), "status" => 1, "level1" => 0, "level2" => 0, "level3" => 0);
        //----判断是否为部室管理者----
        $check_limit = $this->check_room_limit($room_id);
        if (!$check_limit) {
            $this->ajaxReturn($res);
        }
        $res = $this->get_room_award($room_id, $month, $year, $check_limit['level2'], $check_limit['level3']);
        $res = array_merge($check_limit, $res);
        $this->ajaxReturn($res);
    }

    private function check_room_limit($room_id = "", $type = 0)
    {
        $user = session("employee");
        if (empty($user)) {
            //-------返回错误码--------
            return $this->ajaxReturn(['code' => -99]);
        }
        if (empty($room_id)) {
            return false;
        }
        $cond['id'] = $room_id;
        $room = M("ins_room")->where($cond)->find();
        switch ($type) {
            case 0:
                if (($user['id'] != $room['emp_id']) && ($user['id'] != $room['checker']) && ($user['id'] != $room['deside'])) {
                    return false;
                } else {
                    $res['level1'] = ($user['id'] == $room['emp_id']) ? 1 : 0;
                    $res['level2'] = ($user['id'] == $room['checker']) ? 1 : 0;
                    $res['level3'] = ($user['id'] == $room['deside']) ? 1 : 0;
                    return $res;
                }
                break;
            case 1:
                if ($user['id'] != $room['emp_id']) {
                    return false;
                } else {
                    return true;
                }
                break;
            case 2:
                if ($user['id'] != $room['checker']) {
                    return false;
                } else {
                    return true;
                }
                break;
            case 3:
                if ($user['id'] != $room['deside']) {
                    return false;
                } else {
                    return true;
                }
                break;
            case 4:
                if (($user['id'] == $room['detail1']) || ($user['id'] == $room['detail2']) || ($user['id'] == $room['detail3'])) {
                    return true;
                } else {
                    return false;
                }
            default:
                return false;
        }
    }

    private function get_room_award($room_id = "", $month = "", $year = "", $level2 = 0, $level3 = 0)
    {
        $user = session("employee");
        if (empty($user)) {
            //-------返回错误码--------
            return $this->ajaxReturn(['code' => -99]);
        }
        $cond['room_id'] = $room_id;
        $cond['month'] = $month;
        $cond['year'] = $year;
        $cond['status'] = array("NEQ", 9);
        $award = M("ins_award")->where($cond)->find();
        if (empty($award)) {
            return array("code" => -2, "award" => 0, "fee" => 0, "dtl" => array(), "status" => 1, "confirm" => 0);
        }

        //-----自己分配的奖金明细----
        if (($level2 == 0) && ($level3 == 0)) {
            $cond['manager'] = $user['id'];
        }
        $dtl = M("ins_award_dtl")->where($cond)->field("id,emp_id,award")->select();
        $emp_id_arr = array();

        //----该部室分配的所有奖金明细---
        $award_quato = $award['award'];
        $fee_quato = $award['fee'];
        $cond['manager'] = array("NEQ", 0);
        $dtl_withoutmanage = M("ins_award_dtl")->where($cond)->field("id,emp_id,award")->select();
        foreach ($dtl_withoutmanage as $val) {
            if ($award_quato >= $val['award']) {
                $award_quato = round(($award_quato) * 100, 2) / 100 - $val['award'];
                $award_quato = sprintf("%.2f", $award_quato);
            } else {
                $fee_quato = round(($fee_quato) * 100, 2) / 100 + round(($award_quato) * 100, 2) / 100 - $val['award'];
                $award_quato = 0.00;
            }
            $emp_id_arr[] = $val['emp_id'];
        }
        $fee_quato = sprintf("%.2f", $fee_quato);
        foreach ($dtl as $val) {
            $emp_id_arr[] = $val['emp_id'];
        }
        if (!empty($emp_id_arr)) {
            $cond_emp['id'] = array("IN", array_unique($emp_id_arr));
            $emp_arr = M("ins_employee")->where($cond_emp)->getField("id, name");
            foreach ($dtl as &$val) {
                if (array_key_exists($val['emp_id'], $emp_arr)) {
                    $val['name'] = $emp_arr[$val['emp_id']];
                } else {
                    $val['name'] = "";
                }
            }
        }
        $res['code'] = 1;
        $res['award'] = $award_quato;
        $res['fee'] = $fee_quato;
        $res['dtl'] = $dtl;
        $res['status'] = $award['status'];
        $res['confirm'] = $award['confirm'];
        return $res;
    }

    //-----奖金分配-------
    public function ajaxAward()
    {
        $user = session("employee");
        if (empty($user)) {
            //-------返回错误码--------
            return $this->ajaxReturn(['code' => -99]);
        }
        $room_id = I("get.room_id", "", "");
        $emp_id = I("get.emp_id", "", "");
        $month = date("n");
        $year = date("Y");
        $award_get = I("get.award", "", "");
        if (!$this->check_room_limit($room_id, 1)) {
            $this->ajaxReturn(["code" => -1]);
        }
        $cond['room_id'] = $room_id;
        $cond['month'] = $month;
        $cond['year'] = $year;
        $cond['status'] = array("NEQ", 9);
        $award = M("ins_award")->where($cond)->find();
        if (empty($award)) {
            $this->ajaxReturn(["code" => -2]);
        }
        if (empty($emp_id)) {
            $this->ajaxReturn(["code" => -3]);
        }
        //---2.导出，3：已提交待审核，4：已审核待审批，5.流程已完成---
        if ($award['status'] == 2) {
            $this->ajaxReturn(["code" => -5]);
        }
        if ($award['confirm'] == 3) {
            $this->ajaxReturn(["code" => -6]);
        }
        if ($award['status'] == 4) {
            $this->ajaxReturn(["code" => -7]);
        }
        if ($award['status'] == 5) {
            $this->ajaxReturn(["code" => -8]);
        }
        if ($award_get < 0) {
            $this->ajaxReturn(["code" => -4]);
        }
        $dtl = M("ins_award_dtl")->where($cond)->field("emp_id,award")->select();
        //------奖金>分配 || 奖金+费用 > 分配--------
        /*foreach ($dtl as $val) {
            if ($award['award'] >= $val['award']) {
                $award['award'] = $award['award'] - $val['award'];
            } else {
                $award['fee'] = $award['fee'] + $award['award'] - $val['award'];
                if ($award['award'] > 0) {
                    $award['award'] = 0;
                }
            }
        }
        if (($award['award'] < $award_get) && (($award['fee'] + $award['award']) < $award_get)) {
            $this->ajaxReturn(["code" => -4]);
        }*/

        //---------奖金>分配------------
        foreach ($dtl as $val) {
            $award['award'] = $award['award'] - $val['award'];
        }
        if (($award['award'] < $award_get)) {
            $this->ajaxReturn(["code" => -4]);
        }
        $data['emp_id'] = $emp_id;
        $data['month'] = $month;
        $data['year'] = $year;
        $data['room_id'] = $room_id;
        $data['award'] = $award_get;
        $data['manager'] = $user['id'];
        $data['status'] = 1;
        M("ins_award_dtl")->add($data);
        $this->ajaxReturn(["code" => 1]);
    }

    //----奖金分配删除----
    public function ajaxAwardDelete()
    {
        $user = session("employee");
        if (empty($user)) {
            //-------返回错误码--------
            return $this->ajaxReturn(['code' => -99]);
        }
        $id = I("get.id", "", "");
        if (empty($id)) {
            $this->ajaxReturn(["code" => -1]);
        }
        $cond['id'] = $id;
        $dtl = M("ins_award_dtl")->where($cond)->find();
        if (empty($dtl)) {
            $this->ajaxReturn(["code" => -2]);
        }
        if ($dtl['month'] != date("n")) {
            $this->ajaxReturn(["code" => -3]);
        }
        if (!$this->check_room_limit($dtl['room_id'], 1)) {
            $this->ajaxReturn(["code" => -4]);
        }
        $cond_award['room_id'] = $dtl['room_id'];
        $cond_award['year'] = $dtl['year'];
        $cond_award['month'] = $dtl['month'];
//        $cond_award['status'] = 1;
        $award = M("ins_award")->where($cond_award)->find();
        if (empty($award)) {
            $this->ajaxReturn(["code" => -5]);
        }
        if ($award['status'] == 2) {
            $this->ajaxReturn(["code" => -6]);
        }
        if ($award['status'] == 3) {
            $this->ajaxReturn(["code" => -7]);
        }
        if ($award['status'] == 4) {
            $this->ajaxReturn(["code" => -8]);
        }
        if ($award['status'] == 5) {
            $this->ajaxReturn(["code" => -9]);
        }
        $data['status'] = 9;
        M("ins_award_dtl")->where($cond)->save($data);
        $this->ajaxReturn(["code" => 1]);
    }

    //----奖金分配确认提交-----
    public function ajaxAwardConfirm()
    {
        $user = session("employee");
        if (empty($user)) {
            //-------返回错误码--------
            return $this->ajaxReturn(['code' => -99]);
        }
        $room_id = I("get.room_id", "", "");
        if (empty($room_id)) {
            $this->ajaxReturn(["code" => -1]);
        }
        $cond['month'] = date("n");
        $cond['year'] = date("Y");
        $cond['status'] = 1;
        $cond['room_id'] = $room_id;
        $award = M("ins_award")->where($cond)->find();
        if ($award['status'] == 2) {
            $this->ajaxReturn(["code" => -3]);
        }
        if ($award['status'] == 3) {
            $this->ajaxReturn(["code" => -4]);
        }
        if ($award['status'] == 4) {
            $this->ajaxReturn(["code" => -5]);
        }
        if ($award['status'] == 5) {
            $this->ajaxReturn(["code" => -6]);
        }
        $cond_room['id'] = $room_id;
        $room = M("ins_room")->where($cond_room)->find();
        if (empty($room) || ($room['emp_id'] != $user['id'])) {
            $this->ajaxReturn(["code" => -2]);
        }
        $data['confirm'] = 1;
        if ($room['checker'] != 0) {
            $data['status'] = 3;
        } elseif ($room['deside'] != 0) {
            $data['status'] = 4;
        } else {
            $data['status'] = 5;
        }
        $cond_save['id'] = $award['id'];
        M("ins_award")->where($cond_save)->save($data);
        $this->ajaxReturn(["code" => 1]);
    }

    //----奖金分配审核提交-----
    public function ajaxAwardCheck()
    {
        $user = session("employee");
        if (empty($user)) {
            //-------返回错误码--------
            return $this->ajaxReturn(['code' => -99]);
        }
        $room_id = I("get.room_id", "", "");
        $check_result = I("get.check_result", "", "");
        if (empty($room_id)) {
            $this->ajaxReturn(["code" => -1]);
        }
        $cond['month'] = date("n");
        $cond['year'] = date("Y");
        $cond['room_id'] = $room_id;
        $award = M("ins_award")->where($cond)->find();
        if ($award['status'] == 2) {
            $this->ajaxReturn(["code" => -3]);
        }
        if ($award['status'] == 1) {
            $this->ajaxReturn(["code" => -4]);
        }
        if ($award['status'] == 4) {
            $this->ajaxReturn(["code" => -5]);
        }
        if ($award['status'] == 5) {
            $this->ajaxReturn(["code" => -6]);
        }
        $cond_room['id'] = $room_id;
        $room = M("ins_room")->where($cond_room)->find();
        if (empty($room) || ($room['checker'] != $user['id'])) {
            $this->ajaxReturn(["code" => -2]);
        }
        $data['confirm'] = 1;
        if ($check_result == -1) {
            $data['confirm'] = 0;
            $data['status'] = 1;
        } else {
            if ($room['deside'] != 0) {
                $data['status'] = 4;
            } else {
                $data['status'] = 5;
            }
        }
        $cond['month'] = date("n");
        $cond['year'] = date("Y");
        $cond['room_id'] = $room_id;
        M("ins_award")->where($cond)->save($data);
        $this->ajaxReturn(["code" => 1]);
    }

    //----奖金分配审批提交-----
    public function ajaxAwardDeside()
    {
        $user = session("employee");
        if (empty($user)) {
            //-------返回错误码--------
            return $this->ajaxReturn(['code' => -99]);
        }
        $room_id = I("get.room_id", "", "");
        $deside_result = I("get.deside_result", "", "");
        if (empty($room_id)) {
            $this->ajaxReturn(["code" => -1]);
        }
        $cond['month'] = date("n");
        $cond['year'] = date("Y");
        $cond['room_id'] = $room_id;
        $award = M("ins_award")->where($cond)->find();
        if ($award['status'] == 2) {
            $this->ajaxReturn(["code" => -3]);
        }
        if ($award['status'] == 3) {
            $this->ajaxReturn(["code" => -4]);
        }
        if ($award['status'] == 1) {
            $this->ajaxReturn(["code" => -5]);
        }
        if ($award['status'] == 5) {
            $this->ajaxReturn(["code" => -6]);
        }
        $cond_room['id'] = $room_id;
        $room = M("ins_room")->where($cond_room)->find();
        if (empty($room) || ($room['deside'] != $user['id'])) {
            $this->ajaxReturn(["code" => -2]);
        }
        $data['confirm'] = 1;
        if ($deside_result == -1) {
            $data['confirm'] = 0;
            $data['status'] = 1;
        } else {
            $data['status'] = 5;
        }
        $cond['month'] = date("n");
        $cond['year'] = date("Y");
        $cond['room_id'] = $room_id;
        M("ins_award")->where($cond)->save($data);
        $this->ajaxReturn(["code" => 1]);
    }

    public function test()
    {
        set_time_limit(0);
        vendor('phpexcel.PHPExcel');
        $PHPReader = new \PHPExcel();
        $filePath = 'test1.xlsx';
        if (empty($filePath) or !file_exists($filePath)) {
            die('file not exists');
        }
        $objReader = \PHPExcel_IOFactory::createReader("Excel2007");
        $objPHPExcel = $objReader->load($filePath);
        $currentSheet = $objPHPExcel->getSheet(0);
        $allColumn = $currentSheet->getHighestColumn();
        $allRow = $currentSheet->getHighestRow();
        $data = array();
        for ($rowIndex = 2; $rowIndex <= $allRow; $rowIndex++) {        //循环读取每个单元格的内容。注意行从1开始，列从A开始
            $data_unit = array();
            for ($colIndex = 'A'; $colIndex <= $allColumn; $colIndex++) {
                $addr = $colIndex . $rowIndex;
                $cell = $currentSheet->getCell($addr)->getValue();
                if ($cell instanceof PHPExcel_RichText) { //富文本转换字符串
                    $cell = $cell->__toString();
                }
                switch ($colIndex) {
                    case "A":
                        $data_unit['cardnumber'] = $cell;
                        break;
                    case "B":
                        $data_unit['password'] = $cell;
                        break;
                    default:
                        break;
                }
            }
            $data_unit['customer_id'] = -1;
            $data_unit['addr_id'] = -1;
            $data_unit['begintime'] = '2019-10-28 16:55:59';
            $data_unit['endtime'] = '2021-01-01 00:00:00';
            $data_unit['status'] = -1;
            $data_unit['distributionyear'] = '2020';
            $data_unit['rand'] = 0;
            $data[] = $data_unit;
            if (!($rowIndex % 100) || ($rowIndex == $allRow)) {
                M("zjrb_card")->addAll($data);
                $data = array();
            }
        }
        return $data;
    }
}
